
import { resetUserPwd } from '@/api/system/user'
import { ModalForm, ProForm, ProFormText } from '@ant-design/pro-components'
import { message } from 'antd'
import { useRef } from 'react'


const ResetPasswordModal = ({ onFinish, initialValues }) => {
  const formRef = useRef()
  const validateConfirmPass = (_, value) => {
    if (value !== formRef.current.getFieldValue('newPassword')) {
      return Promise.reject('两次输入的密码不一致')
    } else {
      return Promise.resolve()
    }
  }

  const validateNewPass = (_, value) => {
    if (!/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/.test(value)) {
      return Promise.reject('请输入6-20位英文字母、数字或者符号（除空格），且字母、数字和标点符号至少包含两种')
    }
    return Promise.resolve()
  }

  const saveData = (values) => {
    return resetUserPwd(values.id, values.newPassword).then(() => {
      if (res.code !== 200) return;
      onFinish()
      message.success('重置成功')
      return true
    })
  }
  return <ModalForm
    formRef={formRef}
    trigger={<a>重置密码</a>}
    title="重置密码"
    modalProps={{ destroyOnClose: true }}
    onFinish={saveData}
    initialValues={initialValues}
    width={520}
  >

    <ProForm.Item name='id' hidden></ProForm.Item>

    <ProFormText
      name="userName"
      label="用户名"
      fieldProps={{ disabled: true }}
    />

    <ProFormText.Password
      name="newPassword"
      label="新密码"
      placeholder="请输入新密码"
      fieldProps={{ allowClear: true }}
      rules={[{ required: true, message: '请输入新密码' }, { validator: validateNewPass }]}
    />
    <ProFormText.Password
      name="confirmPassword"
      label="确认密码"
      placeholder="请输入确认密码"
      fieldProps={{ allowClear: true }}
      rules={[{ required: true, message: '请输入确认密码' }, { validator: validateConfirmPass }]}
    />
  </ModalForm>
}
export default ResetPasswordModal